/*!
* jquery.countup.js 1.0.3
*
* Copyright 2020 PIGS
*
* PIGS
*
* Date: Oct 27, 2016
*/
(function ($) {
    "use strict";

    $.fn.countUp = function (options) {

        // Defaults
        var settings = $.extend({
            'time': 2000,
            'delay': 10
        }, options);

        return this.each(function () {

            // Store the object
            var $this = $(this);
            var $settings = settings;

            var counterUpper = function () {
                if (!$this.data('counterupTo')) {
                    $this.data('counterupTo', $this.text());
                }
                var time = parseInt($this.data("counter-time")) > 0 ? parseInt($this.data("counter-time")) : $settings.time;
                var delay = parseInt($this.data("counter-delay")) > 0 ? parseInt($this.data("counter-delay")) : $settings.delay;
                var divisions = time / delay;
                var num = $this.data('counterupTo');
                var nums = [num];
                var isComma = /[0-9]+,[0-9]+/.test(num);
                num = num.replace(/,/g, '');
                var isInt = /^[0-9]+$/.test(num);
                var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
                var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;

                // 生成要显示的增量数列表
                for (var i = divisions; i >= 1; i--) {

                    // 如果输入是int，则保持为int
                    var newNum = parseInt(Math.round(num / divisions * i));

                    // 如果输入是浮点型的，则保留浮点型
                    if (isFloat) {
                        newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
                    }

                    // 如果输入有逗号，则保留逗号
                    if (isComma) {
                        while (/(\d+)(\d{3})/.test(newNum.toString())) {
                            newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
                        }
                    }

                    nums.unshift(newNum);
                }

                /**
                 * 统计用户数量
                 *
                 * */
                $.ajax({
                    url: "/countUserNum",
                    type: 'post', //HTTP请求类型
                    success: function (data) {
                        console.log(data)
                        $this.data('counterup-nums', nums);
                        $this.text(data.countUserNum);

                    },
                    error: function () {
                        layer.msg('程序员小哥哥去拿快递了....', {
                            icon: 2,
                            time: 3000, //2秒关闭（如果不配置，默认是3秒）
                            anim: 6
                        });
                    }

                });

                // 更新号码，直到完成
                var f = function () {

                    $this.text($this.data('counterup-nums').shift());
                    if ($this.data('counterup-nums').length) {
                        setTimeout($this.data('counterup-func'), delay);
                    } else {
                        delete $this.data('counterup-nums');
                        $this.data('counterup-nums', null);
                        $this.data('counterup-func', null);
                    }
                };
                $this.data('counterup-func', f);

                // 开始计算
                setTimeout($this.data('counterup-func'), delay);
            };

            // 当元素进入视图时执行计数
            $this.waypoint(counterUpper, {offset: '100%', triggerOnce: true});
        });

    };


})(jQuery);
